package org.alvin.gencode.inters;

import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import lombok.extern.slf4j.Slf4j;
import org.alvin.gencode.AbstractService;
import org.alvin.gencode.beans.PropConfig;
import org.alvin.gencode.run.code.Table;
import org.alvin.gencode.run.code.*;
import org.alvin.gencode.run.service.JDBC2MbTypeService;
import org.alvin.gencode.run.service.VmFileService;
import org.alvin.mini_inject.annotations.MiniComponent;
import org.alvin.mini_inject.annotations.MiniInject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@Slf4j
@MiniComponent
public class GenInterfaceService extends AbstractService {

    @MiniInject
    private CodeDao codeDao;
    @MiniInject
    private PostgreCodeDao postgreCodeDao;

    @MiniInject
    private VmFileService vmFileService;
    @MiniInject
    private JDBC2MbTypeService jdbc2MbTypeService;


    @Override
    public ICodeDao getCodeDaoByType(String dbType) {
        if ("postgre".equals(dbType)) {
            return this.postgreCodeDao;
        }
        return this.codeDao;
    }


    @Override
    public void doCreate(List<Table> tables, PropConfig propConfig) throws SQLException {
        //创建一个word文档，等待填写内容
        Document document = new Document(PageSize.A4);
        try {
            // 创建文件夹
            String outDir = propConfig.getConfigDir().concat(File.separator).concat("dist");
            File dir = new File(outDir);
            dir.mkdirs();
            System.out.println("创建目录：" + dir.getAbsolutePath());
            // 创建文件
            File file = new File(dir, "接口设计文档.docx");
            if (file.exists() && file.isFile()) {
                file.delete();
            }
            file.createNewFile();
            System.out.println("创建文件：" + file.getAbsolutePath());
            //内容填写完，输出文件
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            //打开 doc
            document.open();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //循环遍历输出所有的表的信息
        for (int i = 0; i < tables.size(); i++) {
            Table table = tables.get(i);
            System.out.println("第" + i + "个表:" + table.getComment());
            List<Field> fList = this.getFields(table, propConfig);
            System.out.println("查询字段数量：" + fList.size());
            if (fList == null || fList.isEmpty()) {
                log.error("沒有查詢到字段");
                continue;
            }
            document = this.docAll(document, table, fList);
        }
        //关闭 doc
        document.close();
    }

    private static final String[] interfaceNames = new String[]{
            "信息添加接口",
            "信息查询接口",
            "信息修改接口",
            "信息删除接口",
            "分页查询信息接口"
    };

    private static final String[] logicCommen = new String[]{
            "前端将新增页面字段封装成JSON格式传递到后端，后端写保存方法。insert保存数据。",
            "前端将查询条件传递到后端，后端写查询方法。查询数据返回给前端。（需要排查已经逻辑删除的数据）。",
            "前端将修改页面字段封装成JSON传递到后端，后端写修改方法。update修改数据。",
            "前端将选中的检验项目数据ID传到后端，后端根据ID。修改删除标志字段，隐藏数据。",
            "前端传入的条件和页码信息，查询出符合前段条件的分页列表信息。"
    };


    private Document docAll(Document document, Table dbtable, List<Field> fList) {
        try {
            String title = "数据库表结构：" + dbtable.getT_name() + "系统(" + dbtable.getComment() + "表)";
            Paragraph paragraph = new Paragraph(title, RtfParagraphStyle.STYLE_HEADING_1);
            paragraph.setAlignment(0);
            document.add(paragraph);
            com.lowagie.text.Table table = new com.lowagie.text.Table(3);
            table.setWidth(100);
            table.setBorderWidth(Rectangle.NO_BORDER);
            table.setPadding(0);
            table.setSpacing(0);
            Cell cell = null;
            cell = new Cell("序号");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("接口描述");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("处理逻辑");
            cell.setHeader(true);
            table.addCell(cell);

            for (int i = 0; i < logicCommen.length; i++) {
                Field fi = fList.get(i);
                cell = new Cell((i + 1) + "");
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell(fi.getComment() + interfaceNames[i]);
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell(logicCommen[i]);
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);
            }
            document.add(table);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return document;
    }

}
